package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags="02.微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * 1.发布微博功能
     */
    @ApiOperation("发布微博")
    @PostMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        /*
            1.接收客户端传递的数据;
            2.校验用户登录状态:session.getAttribute("user");
              2.1 未登录状态: return 2;
              2.2 登录状态: 微博表中插入数据[INSERT接口], return 1;
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("weiboDTO = " + weiboDTO);
        log.debug("userVO = " + userVO);
        // 未登录,发布失败
        if (userVO == null){
            return 2;
        }
        // 登录状态,发布成功
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());

        weiboMapper.insert(weibo);

        return 1;
    }

    /**
     * 2.微博首页列表
     */
    @ApiOperation("微博首页列表")
    @GetMapping("selectIndex")
    public List<WeiboIndexVO> selectIndex(){
        return weiboMapper.selectIndex();
    }

    /**
     * 3.微博详情功能
     */
    @ApiOperation("微博详情功能")
    @GetMapping("selectById")
    public WeiboDetailVO selectById(int id){
        System.out.println("id = " + id);
        return weiboMapper.selectById(id);
    }
}









